package com.sdkj.controller.law;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.sdkj.controller.base.BaseController;
import com.sdkj.entity.Page;
import com.sdkj.entity.ScheduleJob;
import com.sdkj.service.base.quartz.JobTaskService;
import com.sdkj.service.law.LawReceiveService;
import com.sdkj.util.AppUtil;
import com.sdkj.util.Const;
import com.sdkj.util.Jurisdiction;
import com.sdkj.util.ObjectExcelView;
import com.sdkj.util.PageData;




@Controller
@RequestMapping(value="/lawreceive")
public class LawReceiveController extends BaseController {
	String menuUrl = "lawreceive/list.do"; //菜单地址(权限用)
	@Resource(name="lawReceiveService")
	private LawReceiveService lawReceiveService;
	
	@Autowired
	private JobTaskService jobTaskService;
	/**
	 * 列表页面
	 */
	@RequestMapping(value="/list")
	public ModelAndView list(Page page){
		logBefore(logger, "列表页面");
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		try {
			pd=this.getPageData();
			page.setPd(pd);
			List<PageData> varList = lawReceiveService.list(page);
			System.out.println("+++++++++++++++++++++++++++++++++");
			mv.setViewName("law/lawReceive/lawReceive_list");
			mv.addObject("varList", varList);
			mv.addObject("pd", pd);
			mv.addObject(Const.SESSION_QX, this.getHC());
		
		} catch (Exception e) {
			logger.error(e.toString(), e);
		}
		return mv;
	}
	/**
	 * 去新增页面
	 */
	@RequestMapping(value="/goAdd")
	public ModelAndView goAdd(){
		logBefore(logger, "去新增页面");
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		try {
			mv.setViewName("law/lawReceive/lawReceive_add");
			mv.addObject("msg", "save");
			mv.addObject("pd", pd);
		} catch (Exception e) {
			logger.error(e.toString(), e);
		}						
		return mv;
	}	
	
	/**
	 * 保存
	 */
	@RequestMapping(value="/save")
	public ModelAndView save(){
		logBefore(logger, "新增");
		if(!Jurisdiction.buttonJurisdiction(menuUrl, "add")){return null;} //校验权限
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		try {
			pd = this.getPageData();
			pd.put("receive_id", this.get32UUID());
			pd.put("user_id", getCurrentUser().getUSER_ID());
			pd.put("receive_person",getCurrentUser().getUSERNAME());
			pd.put("receive_status", 0);
			lawReceiveService.save(pd);
			mv.addObject("msg","success");
			mv.setViewName("save_result");
			
		} catch (Exception e) {
			logger.error(e.toString(), e);
		}
		return mv;
	}
	
	/**
	 * 去往修改页面
	 */
	@RequestMapping(value="goEdit")
	public ModelAndView goEdit(){
		logBefore(logger, "去修改LawReceive页面");
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		try {
			pd = lawReceiveService.findById(pd);	//根据ID读取
			mv.setViewName("law/lawReceive/lawReceive_edit");
			mv.addObject("msg", "edit");
			mv.addObject("pd", pd);
		} catch (Exception e) {
			logger.error(e.toString(), e);
		}						
		return mv;
	}
	/**
	 * 修改
	 */
	@RequestMapping(value="edit")
	public ModelAndView edit()throws Exception{
		logBefore(logger, "审阅");
		if(!Jurisdiction.buttonJurisdiction(menuUrl, "edit")){return null;} //校验权限
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		PageData pd2 = new PageData();
		
		pd = this.getPageData();
		if (!pd.getString("receive_status").equals(pd.getString("status"))) {
		if (pd.getString("receive_status").equals("1")) {
			String string = this.get32UUID();
				pd2.put("revision_id", string);
				pd2.put("user_id", getCurrentUser().getUSER_ID());
				pd2.put("revision_person", getCurrentUser().getUSERNAME());
				pd2.put("receive_id", pd.get("receive_id"));
				pd2.put("revision_status", pd.get("receive_status"));
				pd2.put("revision_begintime", pd.get("revision_begintime"));
				ScheduleJob job = new ScheduleJob();
				String jobId = this.get32UUID(); 
				pd2.put("job_id", jobId);
				job.setJobId(jobId);
				job.setJobName(string);
				job.setJobGroup(string);
				job.setExpression("0/5 * * * * ?");
				job.setJobStatus("1");
				job.setSpringId("lawJob");
				job.setMethodName("remind");
				lawReceiveService.saveRev(pd2);
				lawReceiveService.updateRe(pd);
				jobTaskService.addJob(job);
			
		}else if (pd.getString("receive_status").equals("2")) {
			pd2.put("revision_id", pd.get("revision_id"));
			pd2.put("user_id", getCurrentUser().getUSER_ID());
			pd2.put("revision_person", getCurrentUser().getUSERNAME());
			pd2.put("receive_id", pd.get("receive_id"));
			pd2.put("revision_status", pd.get("receive_status"));
			pd2.put("revision_begintime", pd.get("revision_begintime"));
			pd2.put("revision_endtime", pd.get("revision_endtime"));
			lawReceiveService.updateRev(pd2);
			lawReceiveService.updateRe(pd);
			jobTaskService.deleteJob(lawReceiveService.findJobById(pd).getString("job_name"), 
					lawReceiveService.findJobById(pd).getString("job_group"));
		}
		}
		mv.addObject("msg","success");
		mv.setViewName("save_result");
		return mv;
	}
	/**
	 * 删除
	 */
	@RequestMapping(value="/delete")
	public void delete(PrintWriter out){
		logBefore(logger, "删除LawReceive");
		if(!Jurisdiction.buttonJurisdiction(menuUrl, "del")){return;} //校验权限
		PageData pd = new PageData();
		try{
			pd = this.getPageData();
			lawReceiveService.delete(pd);
			out.write("success");
			out.close();
		} catch(Exception e){
			logger.error(e.toString(), e);
		}
	}
	/**
	 * 批量删除
	 */
	@RequestMapping(value="/deleteAll")
	@ResponseBody
	public Object deleteAll() {
		logBefore(logger, "批量删除LawReceive");
		if(!Jurisdiction.buttonJurisdiction(menuUrl, "dell")){return null;} //校验权限
		PageData pd = new PageData();		
		Map<String,Object> map = new HashMap<String,Object>();
		try {
			pd = this.getPageData();
			List<PageData> pdList = new ArrayList<PageData>();
			String DATA_IDS = pd.getString("DATA_IDS");
			if(null != DATA_IDS && !"".equals(DATA_IDS)){
				String ArrayDATA_IDS[] = DATA_IDS.split(",");
				lawReceiveService.deleteAll(ArrayDATA_IDS);
				pd.put("msg", "ok");
			}else{
				pd.put("msg", "no");
			}
			pdList.add(pd);
			map.put("list", pdList);
		} catch (Exception e) {
			logger.error(e.toString(), e);
		} finally {
			logAfter(logger);
		}
		return AppUtil.returnObject(pd, map);
	}
	/*
	 * 导出到excel
	 * @return
	 */
	@RequestMapping(value="/excel")
	public ModelAndView exportExcel(){
		logBefore(logger, "导出LawReceive到excel");
		if(!Jurisdiction.buttonJurisdiction(menuUrl, "cha")){return null;}
		ModelAndView mv = new ModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		try{
			Map<String,Object> dataMap = new HashMap<String,Object>();
			List<String> titles = new ArrayList<String>();
			titles.add("");	//1
			titles.add("");	//2
			titles.add("");	//3
			titles.add("");	//4
			titles.add("");	//5
			titles.add("");	//6
			titles.add("");	//7
			dataMap.put("titles", titles);
			List<PageData> varOList = lawReceiveService.listAll(pd);
			List<PageData> varList = new ArrayList<PageData>();
			for(int i=0;i<varOList.size();i++){
				PageData vpd = new PageData();
				vpd.put("var1", varOList.get(i).getString("receive_id"));	//1
				vpd.put("var2", varOList.get(i).getString("receive_type"));	//2
				vpd.put("var3", varOList.get(i).getString("receive_time"));	//3
				vpd.put("var4", varOList.get(i).getString("receive_attachment"));	//4
				vpd.put("var5", varOList.get(i).getString("receive_status"));	//5
				vpd.put("var6", varOList.get(i).getString("user_id"));	//6
				vpd.put("var7", varOList.get(i).getString("company_Id"));	//7
				varList.add(vpd);
			}
			dataMap.put("varList", varList);
			ObjectExcelView erv = new ObjectExcelView();
			mv = new ModelAndView(erv,dataMap);
		} catch(Exception e){
			logger.error(e.toString(), e);
		}
		return mv;
	}
	
	
	
	
	
	
	
	
	/* ===============================权限================================== */
	public Map<String, String> getHC(){
		Subject currentUser = SecurityUtils.getSubject();  //shiro管理的session
		Session session = currentUser.getSession();
		return (Map<String, String>)session.getAttribute(Const.SESSION_QX);
	}
/* ===============================权限================================== */
	
	@InitBinder
	public void initBinder(WebDataBinder binder){
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		binder.registerCustomEditor(Date.class, new CustomDateEditor(format,true));
	}
	
	
	
	
}
